import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import  java.lang.*;

/*
使用 curl -v http://localhost:8080/test/First -X POST
 */
@WebServlet("First")
public class First extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        try(Connection conn=Final.getConnect("jdbc/DSTest"))
        {
            Statement stmt=conn.createStatement();
            stmt.executeUpdate(
                    "CREATE TABLE IF NOT EXISTS `student` ("+
                            "  `counter` int(11) AUTO_INCREMENT NOT NULL,"+
                            "  `num` int(11) unsigned NOT NULL," +
                            "  `name` varchar(30) NOT NULL," +
                            "  `score` int(11) NOT NULL," +
                            "  PRIMARY KEY (`counter`)" +
                            ");"
            );

            stmt.executeUpdate(
                    "insert into `student` (`num`,`name`,`score`) values (1, 'xixi', 60);" +
                            "insert into `student` (`num`,`name`,`score`) values (2, 'pingping', 70);"+
                            "insert into `student` (`num`,`name`,`score`) values (3, 'xinxin', 80);"
            );

            response.setContentType("text/html;charset=UTF-8");
            ResultSet rs=stmt.executeQuery("select * from student order by counter desc limit 0,3");
            while(rs.next())
            {
                response.getWriter().println(rs.getInt("num")+"  "+rs.getString("name")+"  "+rs.getInt("score"));
            }
        }
        catch (Exception e) {response.getWriter().println("error!");}
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doPost(request,response);
    }
}
